<?php
namespace app\admin\controller;

use taobao\AliOss;
use think\Db;
use app\enumerations\SceneStatusEnum;

class SceneItem extends Common {

    /**
     * 场景小节列表
     * @return mixed
     */
    public function index() {

        $sceneId = $this->request->param('scene_id');

        $scene = $this->sceneService->findById($sceneId);

        if (isNullOrEmpty($scene)) {
            $this->error('场景不存在');
        }

        $list = $this->sceneItemService->getSceneItemList($sceneId);

        $this->assign('list', $list);
        $this->assign('scene', $scene);
        return $this->fetch();

    }

    /**
     * 上传图片
     * @return string
     */
    public function uploadImg() {

        $post = $this->request->post();

        $tempFile = $_FILES['file']['tmp_name'];

        $fileName = md5(uniqid(mt_rand(), true)).".".strtolower(pathinfo($_FILES['file']['name'])["extension"]);

        $info = AliOss::uploadFile($tempFile,$fileName);

        $url = $info['info']['url'];
        $start = strrpos($url,"/");
        $imgName = substr($url, $start);
        $iconPath = config('oss.Cname').$imgName;

        $maxSort = $this->sceneItemService->getMaxSortBySceneId($post['scene_id']);

        $data['scene_id'] = $post['scene_id'];
        $data['icon_path'] = $iconPath;
        $data['sort'] = isNullOrEmpty($maxSort) ? 5 : $maxSort['sort']+5;

        Db::startTrans();
        try {

            $this->sceneItemService->saveByData($data);

            $itemList = $this->sceneItemService->getSceneItemListForApi($post['scene_id']);

            $sceneData['item_json'] = json_encode($itemList);
            $this->sceneService->updateByIdAndData($post['scene_id'], $sceneData);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            $this->error('服务器错误,请稍后再试');

        }

        $this->success('上传成功');

    }

    /**
     * 排序操作
     */
    public function order() {

        $param = input('post.');

        $scene = $this->sceneService->findById($param['scene_id']);

        if (isNullOrEmpty($scene)) {
            $this->error('场景不存在');
        }

        $updateList = array();

        foreach ($param as $id => $sort) {
            $info["id"] = $id;
            $info["sort"] = $sort;

            array_push($updateList, $info);
        }

        Db::startTrans();
        try {

            $this->sceneItemService->updateByList($updateList);

            $itemList = $this->sceneItemService->getSceneItemListForApi($scene['id']);

            $sceneData['item_json'] = json_encode($itemList);
            $this->sceneService->updateByIdAndData($scene['id'], $sceneData);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            $this->error('服务器错误,请稍后再试');

        }

        $this->success("排序更新成功");

    }

    /**
     * 软删除
     */
    public function delete() {

        $id = $this->request->param('id');

        $sceneItem = $this->sceneItemService->findById($id);

        if (isNullOrEmpty($sceneItem)) {
            $this->error('数据不存在');
        }

        $data['status'] = SceneStatusEnum::getEnumValue(SceneStatusEnum::ABANDON_CODE);

        Db::startTrans();
        try {

            $this->sceneItemService->updateByIdAndData($id, $data);

            $itemList = $this->sceneItemService->getSceneItemListForApi($sceneItem['scene_id']);

            $sceneData['item_json'] = json_encode($itemList);
            $this->sceneService->updateByIdAndData($sceneItem['scene_id'], $sceneData);

            Db::commit();

        } catch(\PDOException $e) {

            Db::rollback();

            $this->error('服务器错误,请稍后再试');

        }

        $this->success('删除成功');

    }

}